# plugs/topic.py
#
#

""" topic management """

__copyright__ = 'this file is in the public domain'

from gozerbot.commands import cmnds
from gozerbot.examples import examples
from gozerbot.plughelp import plughelp
import Queue, time

plughelp.add('topic', 'manage the topic of a channel')

def gettopicdata(bot, channel):
    """ get topic data """
    queue332 = Queue.Queue()
    queue333 = Queue.Queue()
    bot.wait.register('332', channel, queue332)
    bot.wait.register('333', channel, queue333)
    bot.send('TOPIC %s' % channel)
    try:
        res = queue332.get(1, 5)
    except Queue.Empty:
        return None
    what = res.txt
    try:
        res = queue333.get(1, 5)
    except Queue.Empty:
        return None
    try:
        splitted = res.postfix.split()
        who = splitted[2]
        when = float(splitted[3])
    except (IndexError, ValueError):
        return None
    return (what, who, when)

def handle_gettopic(bot, ievent):
    """ topic [<channel>] .. get topic """
    try:
        channel = ievent.args[0]
    except IndexError:
        channel = ievent.channel
    result = gettopicdata(bot, channel)
    if not result:
        ievent.reply("can't get topic data of channel %s" % channel)
    else:
        (what, who, when) = result
        ievent.reply('topic on %s is %s made by %s on %s' % \
(channel, what, who, time.ctime(when)))

cmnds.add('topic', handle_gettopic, 'USER')
examples.add('topic', 'topic [<channel>] .. get topic', '1) topic 2) topic \
#dunkbots')

def handle_topicset(bot, ievent):
    """ topic-set .. set the topic """
    if not ievent.rest:
        ievent.missing('<what>')
        return
    bot.send('TOPIC %s :%s' % (ievent.channel, ievent.rest))

cmnds.add('topic-set', handle_topicset, 'USER')
examples.add('topic-set', 'set channel topic', 'topic-set Yooo')

def handle_topicadd(bot, ievent):
    """ topic-add <txt> .. add topic item """
    if not ievent.rest:
        ievent.missing("<what>")
        return
    result = gettopicdata(bot, ievent.channel)
    if not result:
        ievent.reply("can't get topic data")
        return
    what = result[0]
    what += " | %s" % ievent.rest
    bot.send('TOPIC %s :%s' % (ievent.channel, what))

cmnds.add('topic-add', handle_topicadd, 'USER')
examples.add('topic-add', 'topic-add <txt> .. add a topic item', \
'topic-add mekker')

def handle_topicdel(bot, ievent):
    """ topic-del <topicnr> .. delete topic item """
    try:
        topicnr = int(ievent.args[0])
    except (IndexError, ValueError):
        ievent.reply('i need a integer as argument')
        return
    if topicnr < 1:
        ievent.reply('topic items start at 1')
        return
    result = gettopicdata(bot, ievent.channel)
    if not result:
        ievent.reply("can't get topic data")
        return
    what = result[0].split(' | ')
    if topicnr > len(what):
        ievent.reply('there are only %s topic items' % len(what))
        return
    del what[topicnr-1]
    newtopic = ' | '.join(what)
    bot.send('TOPIC %s :%s' % (ievent.channel, newtopic))

cmnds.add('topic-del', handle_topicdel, 'USER')
examples.add('topic-del', 'topic-del <topicnr> .. delete topic item', \
'topic-del 1')

def handle_topicmove(bot, ievent):
    """ topic-move <nrfrom> <nrto> .. move topic item """
    try:
        (topicfrom, topicto) = ievent.args
    except ValueError:
        ievent.missing('<from> <to>')
        return
    try:
        topicfrom = int(topicfrom)
        topicto = int(topicto)
    except ValueError:
        ievent.reply('i need two integers as arguments')
        return
    if topicfrom < 1 or topicto < 1:
        ievent.reply('topic items start at 1')
        return
    topicdata = gettopicdata(bot, ievent.channel)
    if not topicdata:
        ievent.reply("can't get topic data")
        return
    splitted = topicdata[0].split(' | ')
    if topicfrom > len(splitted) or topicto > len(splitted):
        ievent.reply('max item is %s' % len(splitted))
        return
    tmp = splitted[topicfrom-1]
    del splitted[topicfrom-1]
    splitted.insert(topicto-1, tmp)
    newtopic = ' | '.join(splitted)
    bot.send('TOPIC %s :%s' % (ievent.channel, newtopic))

cmnds.add('topic-move', handle_topicmove, 'USER')
examples.add('topic-move', 'topic-move <nrfrom> <nrto> .. move topic \
items', 'topic-move 3 1')
